package com.lemner.www.hongyu.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.provider.CalendarContract;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.lemner.www.hongyu.Activity.FinishrepairActivity;
import com.lemner.www.hongyu.Bean.BegintorepairBean;
import com.lemner.www.hongyu.Bean.FinishedrepairBean;
import com.lemner.www.hongyu.Bean.GetdepartpersonBean;
import com.lemner.www.hongyu.Bean.TranorderBean;
import com.lemner.www.hongyu.Bean.UnfinishorderBean;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 小博 on 2017/11/7.
 */

public class UnfinishorderAdater extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private UnfinishorderBean.DataBean bean;
    private Retrofit retrofit;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    private int communityid,workerid;
    private String name,department;
    private OptionsPickerView PvDepartment;//部门选择器
    private ArrayList<GetdepartpersonBean.DataBean.ListBean> options1Items = new ArrayList<>();//部门
    private int option,orderid, workerPerson_id;
    private int flag,flags,num;
    public UnfinishorderAdater(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }
    public void setDate(UnfinishorderBean.DataBean bean){
        this.bean=bean;
        notifyDataSetChanged();
    }
    public void setId(int communityid ,int workerid){
        this.communityid=communityid;
        this.workerid=workerid;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_unfinishorder, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, final int position) {
        UnfinishorderBean.DataBean.ListBean date=bean.getList().get(position);
        final ViewHolder holder1=(ViewHolder) holder;
        if(date==null){
            return;
        }
        holder1.tv_worker.setText(date.getOwner_name());
        holder1.tv_cause1.setText(date.getCause());
        holder1.tv_address1.setText(date.getAddress());
        holder1.tv_appointtime.setText(date.getAppointment_time());
        holder1.tv_iphone.setText(date.getOwner_phone());
        holder1.tv_ordernum1.setText(date.getOrder_number());
        holder1.tv_zhuandan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDate();
            }
        });
        num=bean.getList().get(position).getProgress();
        if(num==2) {
            holder1.ll_background.setBackgroundResource(R.color.colorAccent);
            holder1.tv_weixiu.setText("开始维修");
            holder1.tv_weixiu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    orderid=bean.getList().get(position).getOrder_id();
                    begin();
                    if (flag == 1) {
                        holder1.ll_background.setBackgroundResource(R.color.blue);
                        holder1.tv_weixiu.setText("完成维修");
                        num = 3;
                    }
                    if(num==3){
                        holder1.ll_background.setBackgroundResource(R.color.blue);
                        holder1.tv_weixiu.setText("完成维修");
                        holder1.tv_weixiu.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                complete();
                                if(flags==0){
                                    Intent intent=new Intent(context,FinishrepairActivity.class);
                                    intent.putExtra("worker_id",workerid);
                                    intent.putExtra("order_id",orderid);
                                    context.startActivity(intent);
                                }
                            }
                        });
                    }
                }
            });
        }
        else{
            orderid=bean.getList().get(position).getOrder_id();
            holder1.ll_background.setBackgroundResource(R.color.blue);
            holder1.tv_weixiu.setText("完成维修");
            holder1.tv_weixiu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    complete();
                    if(flags==0){
                        Intent intent=new Intent(context,FinishrepairActivity.class);
                        intent.putExtra("worker_id",workerid);
                        intent.putExtra("order_id",orderid);
                        context.startActivity(intent);
                    }
                }
            });
        }

    }
    @Override
    public int getItemCount() {
        if(bean==null)
        return 0;
        else
            return bean.getList().size();
    }

    public class  ViewHolder extends  RecyclerView.ViewHolder {
        private TextView tv_ordernum1,tv_zhuandan,tv_weixiu,tv_worker,tv_iphone,tv_appointtime,
        tv_address1,tv_cause1;
        private LinearLayout ll_background;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_ordernum1=(TextView)itemView.findViewById(R.id.tv_ordernum1);
            tv_address1=(TextView)itemView.findViewById(R.id.tv_address1);
            tv_appointtime=(TextView)itemView.findViewById(R.id.tv_appointtime);
            tv_cause1=(TextView)itemView.findViewById(R.id.tv_cause1);
            tv_zhuandan=(TextView)itemView.findViewById(R.id.tv_zhuandan);
            tv_iphone=(TextView)itemView.findViewById(R.id.tv_iphone);
            tv_weixiu=(TextView)itemView.findViewById(R.id.tv_weixiu);
            tv_worker=(TextView)itemView.findViewById(R.id.tv_worker);
            ll_background=(LinearLayout)itemView.findViewById(R.id.ll_background);
        }
    }

    public void Selectdepartment(){
        PvDepartment = new  OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                /* options1Items.get(options1);*/
                 option=options1;
                 tranOrder();
            }
        })
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(18)//确定和取消文字大小
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setBgColor(0xFFFFFFFF)//滚轮背景颜色 Night mode
                .setContentTextSize(18)//滚轮文字大小
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(1, 1, 1)  //设置默认选中项
                .setOutSideCancelable(false)//点击外部dismiss default true
                .isDialog(true)//是否显示为对话框样式
                .build();
        PvDepartment.setPicker(options1Items);
    }

//获取部门人员
    public void getDate(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
       Call<GetdepartpersonBean>call=service.getStaff(communityid,workerid);
        call.enqueue(new Callback<GetdepartpersonBean>() {
            @Override
            public void onResponse(Call<GetdepartpersonBean> call, Response<GetdepartpersonBean> response) {
                options1Items.addAll(response.body().getData().getList());
                Selectdepartment();
                PvDepartment.show();
                workerPerson_id=response.body().getData().getList().get(option).getId();
            }
            @Override
            public void onFailure(Call<GetdepartpersonBean> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });
    }

    //转单
public void tranOrder(){
    retrofit=new Retrofit.Builder()
            .baseUrl(BASEURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    service=retrofit.create(Service.class);
    Call<TranorderBean>call=service.getTran(orderid,workerPerson_id,workerid);
    call.enqueue(new Callback<TranorderBean>() {
        @Override
        public void onResponse(Call<TranorderBean> call, Response<TranorderBean> response) {
            if(response.body().getData().getSuccess()==1)
            Toast.makeText(context,"转单成功",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<TranorderBean> call, Throwable t) {
            Log.e("error", t.getMessage());
        }
    });
}

//开始维修
    public void begin(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
        Call<BegintorepairBean>call=service.getBegin(orderid,workerid);
        call.enqueue(new Callback<BegintorepairBean>() {
            @Override
            public void onResponse(Call<BegintorepairBean> call, Response<BegintorepairBean> response) {
               flag=response.body().getData().getSuccess();
        }
            @Override
            public void onFailure(Call<BegintorepairBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }
    //完成维修
    public void complete(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(Service.class);
       Call<FinishedrepairBean>call= service.getFinish(orderid,workerid);
        call.enqueue(new Callback<FinishedrepairBean>() {
            @Override
            public void onResponse(Call<FinishedrepairBean> call, Response<FinishedrepairBean> response) {
             flags=response.body().getData().getSuccess();
            }

            @Override
            public void onFailure(Call<FinishedrepairBean> call, Throwable t) {
                Log.e("error",t.getMessage());
            }
        });
    }

}
